package com.hnqcgczy.dao.imp;

import com.hnqcgczy.dao.BaseDao;
import com.hnqcgczy.dao.IOrderDao;
import com.hnqcgczy.db.MyTransaction;
import com.hnqcgczy.vo.OrderDetailVo;
import com.hnqcgczy.vo.TOrder;
import com.hnqcgczy.vo.TOrderDetail;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements IOrderDao {
    //定义事务
    MyTransaction myTransaction;
    //构造方法， 完成类的初始化
    public OrderDaoImpl(MyTransaction myTransaction){
        //给BaseDao
        super(myTransaction);
        this.myTransaction=myTransaction;
    }
    @Override
    public int addOrder(TOrder order) {
        //编写预编译SQL语句
        String sql="insert into t_order values(null,?,?,?,?,?,?,?)";
        //执行SQL语句
        int count =this.update(sql,order.getToNumber(),order.getTuId(),order.getToUserAddress(),order.getToCreateTime(),
                order.getToCost(),order.getToStatus(),order.getToType());
        // 释放资源
        this.closeResource();
        //返回结果
        return count;
    }

    @Override
    public int addOrderDetail(TOrderDetail orderDetail) {
        //编写预编译SQL
        String sql="insert into t_order_detail values(null,?,?,?,?)";
        //执行SQL语句
        int count=this.update(sql,orderDetail.getToNumber(),orderDetail.getTpId(),
                orderDetail.getTodQuantity(),orderDetail.getTodCost());
        //释放资源
        this.closeResource();
        //返回结果
        return count;
    }

    @Override
    public List<TOrder> getOrderList(String tuId) {
        //编写预编译SQL语句
        String sql="select * from t_order where tu_id=?";
        //返回结果集合
        List<TOrder> list=new ArrayList<TOrder>();
        QueryRunner queryRunner=new QueryRunner();
        //处理下划线转换成驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);

        try {
            list=queryRunner.query(myTransaction.getConn(),sql,
                    new BeanListHandler<TOrder>(TOrder.class,rowProcessor),tuId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //返回结果
        return list;
    }

    @Override
    public List<OrderDetailVo> getOrderDetailList(String oNumber) {
        //预编译SQL语句, 因为需要获取商品信息， 所以需要连表查询
        String sql="select * from t_order_detail od ,t_product p where od.tp_id=p.tp_id and od.to_number =?";
        //定义返回结果集合
        List<OrderDetailVo> list=new ArrayList<OrderDetailVo>();
        QueryRunner queryRunner=new QueryRunner();
        //处理下划线和驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);

        try {
            list=queryRunner.query(myTransaction.getConn(),sql,
                    new BeanListHandler<OrderDetailVo>(OrderDetailVo.class,rowProcessor),oNumber);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int updadeOrder(TOrder order) {
        //编写预编译SQL语句
        String sql="update t_order set to_status=? where to_number =?";
        //执行SQL语句
        int count =this.update(sql,order.getToStatus(),order.getToNumber());
        // 释放资源
        this.closeResource();
        //返回结果
        return count;
    }

    @Override
    public TOrder getOrder(String oNumber) {
        //预编译SQL语句, 因为需要获取商品信息， 所以需要连表查询
        String sql="select * from t_order where  to_number =?";
        //定义返回结果集合
        QueryRunner queryRunner=new QueryRunner();
        //处理下划线和驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        TOrder order=new TOrder();
        try {
            order=queryRunner.query(myTransaction.getConn(),sql,
                    new BeanHandler<TOrder>(TOrder.class,rowProcessor),oNumber);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }
}
